<?php

namespace App\Admin\Controllers;

use App\Models\DrawLotsConfig;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\MessageBag;

class DrawLotsConfigController extends Controller
{
    use ModelForm;
    

    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('配置抽签');

            $content->body($this->grid());
        });
    }


    protected function grid()
    {
        return Admin::grid(DrawLotsConfig::class, function (Grid $grid) {

            $grid->name('配置项');

            $grid->disableCreateButton();
            $grid->actions(function($grid){
            });
        });
    }

    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑配置');

            $content->body($this->form()->edit($id));
        });
    }

    protected function form()
    {
        return Admin::form(DrawLotsConfig::class, function (Form $form) {

            $form->number('luckyMaxScore','大吉得分');
            $form->rate('luckyMaxRate','大吉占比');
            $form->number('luckyUpScore','上吉得分');
            $form->rate('luckyUpRate','上吉占比');
            $form->number('luckyMiddleScore','中吉得分');
            $form->rate('luckyMiddleRate','中吉占比');
            $form->number('luckyMinScore','小吉得分');
            $form->rate('luckyMinRate','小吉占比');
            $form->number('safeScore','平安得分');
            $form->rate('safeRate','平安占比');
            $form->number('drawLotsFiveDaysScore','累计抽签5天得分');
            $form->number('dayJoinNum','预计每日参与人数');

            $form->saved(function ($form) {
                //将文件存至本地
                Storage::put('draw_lots_config.json',$form->model()->toJson());

                $success = new MessageBag([
                    'title'   => '保存成功',
                ]);
                return back()->with(compact('success'));
            });
        });
    }
}